<?php 
  namespace App\Http\Controllers\Service;
  use App\Http\Controllers\Controller;
  //引入状态输出类
  use App\Models\M3Result;
  use Illuminate\Http\Request;
  use App\Entity\Cart;
  use App\Entity\OrderReal;
  use App\Entity\Product;
  use App\Tool\UUID;
  /**
   * 
  * coder:zhengchengjun
  * time:2017-9-12
  * function:订单提交类
  */
  class OrderController extends Controller{
    //订单生效时间为订单提交30天后
    private $liveTime=2592000;
    public function toOrder(Request $request){
      if (!$request->session()->has('member')) {
        //回到后台登录页
        return redirect('login');
      }
      $cartDb=new Cart;
      $result=new M3Result;
      $productDb=new Product;
      $orderrealDb=new orderReal;
      $uid=$request->session()->get('member',' ');
      $pid=$request->input('pid',' ');
      $pidArr=$pid?explode(',',$pid):array();
      $phone=$request->input('phone',' ');
      $address=$request->input('address',' ');
      $ps=$request->input('ps',' ');
      $user=$request->input('user',' ');
      //返回的是32位的字符串
      $orderCode=UUID::create();
      //截取为16位
      $orderCode=substr(md5($orderCode),0,16);
      //主副表好像反了
      //产品表连接购物车表
      //用户的购物车表pid对应到产品id（where），然后挑选一些指定的产品()
      $value=$productDb
       ->leftJoin('cart', 'product.id', '=', 'cart.pid')
            ->select('product.id','product.name','product.preview','product.price', 'cart.count', 'cart.specifications')
            ->whereIn('product.id',$pidArr)
            ->where('mid',$uid)
            ->get();
      $orderProductinfo=$value->toJson();
      $orderPrice=0;
      foreach($value as $k=>$v){
        $orderPrice+=$v['price']*$v['count'];
      }
      $orderrealDb->member_id=$uid;
      $orderrealDb->total_price=$orderPrice;
      $orderrealDb->phone=$phone;
      $orderrealDb->address=$address;
      $orderrealDb->tracking_user=$user;
      $orderrealDb->orderStatus=0;
      $orderrealDb->ordercode=$orderCode;
      $orderrealDb->creat_time_int=time();
      $orderrealDb->dedeTime=time()+$this->liveTime;
      $orderrealDb->product_info=$orderProductinfo;
      $orderrealDb->ps=$ps;
      $id=$orderrealDb->save();
      $result->orderCode=$orderCode;
      $result->status=0;
      return $result->toJson();
    }
    /**
     * 返回用户订单的json
     * @param  Request $request [description]
     * @param  integer $status  [description]
     * @return [type]           [description]
     */
     public function memberOrder(Request $request,$status=1){
      if (!$request->session()->has('member')) {
        //回到后台登录页
        return redirect('login');
      }
      $orderrealDb=new OrderReal();
      $uid=$request=$request->session()->get('member',' ');
      switch ($status) {
        case '0':
          $result=$orderrealDb->select('id','total_price','orderStatus','phone','address','product_info','ordercode','created_at')->where('member_id',$uid)->orderby('creat_time_int','desc')->simplePaginate(8);
        break;
        case '1':
          $result=$orderrealDb->select('id','total_price','orderStatus','phone','address','product_info','ordercode','created_at')->where('member_id',$uid)->where('orderStatus',0)->orderby('creat_time_int','desc')->simplePaginate(8);
        break;
        case '2':
          $result=$orderrealDb->select('id','total_price','orderStatus','phone','address','product_info','ordercode','created_at')->where('member_id',$uid)->where('orderStatus',1)->orderby('creat_time_int','desc')->simplePaginate(8);
        break;
        case '3':
          $result=$orderrealDb->select('id','total_price','orderStatus','phone','address','product_info','ordercode','created_at')->where('member_id',$uid)->where('orderStatus',2)->orderby('creat_time_int','desc')->simplePaginate(8);
        break;
      }
      return $result->toJson();
    }
    /**
     * 删除订单
     */
    public function delOrder(Request $request){
      if (!$request->session()->has('member')) {
        //回到后台登录页
        return redirect('login');
      }
      $orderrealDb=new OrderReal;
      $result=new M3Result;
      $oid=$request->input('oid',' ');
      $uid=$request->session()->get('member',' ');
      $mid=$orderrealDb->where('id',$oid)->pluck('member_id');
      if($uid!=$mid){
        return view('errors.404');
      }else{
        $orderrealDb->where('id',$oid)->delete();
        $result->message='删除订单成功';
        $result->status=1;
        return $result->toJson();
      }
    }
    public function conFirmOrder(Request $request){
      if (!$request->session()->has('member')) {
        //回到后台登录页
        return redirect('login');
      }
      $result=new M3Result;
      $oid=$request->input('oid',' ');
      $uid=$request->session()->get('member',' ');
      $orderrealDb=OrderReal::find($oid);
      $orderrealDb->orderStatus=2;
      $orderrealDb->save();
      $result->message='删除订单成功';
      $result->status=1;
      return $result->toJson();
    }
  }
 ?>
